/**
 * Vue Router 配置
 * 
 * 定义应用程序的路由结构和导航逻辑
 * 支持主窗口和子窗口的不同路由模式
 */

import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'

// 导入页面组件
import Welcome from '../views/Welcome.vue'
import Settings from '../views/Settings.vue'
import SystemIntegration from '../views/SystemIntegration.vue'
import HardwareAccess from '../views/HardwareAccess.vue'
import NetworkFeatures from '../views/NetworkFeatures.vue'
import StorageFeatures from '../views/StorageFeatures.vue'
import SecurityFeatures from '../views/SecurityFeatures.vue'
import { 
  FileManager, 
  IPCDemo, 
  CommunicationExample, 
  ChildWindow,
  BroadcastDebug
} from '../components/demos'

/**
 * 路由配置
 * 使用hash模式以避免Electron中的路径问题
 */
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Welcome',
    component: Welcome,
    meta: {
      title: '欢迎页面',
      icon: '🏠'
    }
  },
  {
    path: '/file-manager',
    name: 'FileManager',
    component: FileManager,
    meta: {
      title: '文件管理',
      icon: '📁'
    }
  },
  {
    path: '/ipc-demo',
    name: 'IPCDemo',
    component: IPCDemo,
    meta: {
      title: 'IPC通信',
      icon: '🔄'
    }
  },
  {
    path: '/communication',
    name: 'Communication',
    component: CommunicationExample,
    meta: {
      title: '通信实战',
      icon: '📡'
    }
  },
  {
    path: '/multi-window',
    name: 'MultiWindow',
    component: () => import('../views/MultiWindow.vue'),
    meta: {
      title: '多窗口演示',
      icon: '🪟'
    }
  },
  {
    path: '/system-integration',
    name: 'SystemIntegration',
    component: SystemIntegration,
    meta: {
      title: '系统集成',
      icon: '🔧'
    }
  },
  {
    path: '/hardware-access',
    name: 'HardwareAccess',
    component: HardwareAccess,
    meta: {
      title: '硬件访问',
      icon: '🖥️'
    }
  },
  {
    path: '/network-features',
    name: 'NetworkFeatures',
    component: NetworkFeatures,
    meta: {
      title: '网络功能',
      icon: '🌐'
    }
  },
  {
    path: '/storage-features',
    name: 'StorageFeatures',
    component: StorageFeatures,
    meta: {
      title: '存储功能',
      icon: '💾'
    }
  },
  {
    path: '/security-features',
    name: 'SecurityFeatures',
    component: SecurityFeatures,
    meta: {
      title: '安全功能',
      icon: '🔒'
    }
  },
  {
    path: '/broadcast-debug',
    name: 'BroadcastDebug',
    component: BroadcastDebug,
    meta: {
      title: '广播调试',
      icon: '🔍'
    }
  },
  {
    path: '/settings',
    name: 'Settings',
    component: Settings,
    meta: {
      title: '设置',
      icon: '⚙️'
    }
  },
  {
    path: '/child-window',
    name: 'ChildWindow',
    component: ChildWindow,
    meta: {
      title: '子窗口',
      icon: '🪟',
      isChildWindow: true
    }
  }
]

/**
 * 创建路由实例
 * 使用Hash模式确保在Electron环境中正常工作
 */
const router = createRouter({
  history: createWebHashHistory(),
  routes
})

/**
 * 路由守卫 - 设置页面标题
 */
router.beforeEach((to) => {
  // 根据路由设置窗口标题
  if (to.meta?.isChildWindow) {
    document.title = `${to.meta.title} - Electron应用`
  } else {
    document.title = to.meta?.title ? `${to.meta.title} - yuCloudEle` : 'yuCloudEle'
  }
})

export default router

/**
 * 导出路由配置供其他组件使用
 */
export const routeConfig = routes.filter(route => !route.meta?.isChildWindow)
